<?php

include_once APPLICATION_DIR . "/application/model/CONNECTOR.php";

class Connector extends DataObjects_CONNECTOR
{
	function getList($filter = array(), &$pager=false)
	{
		$connector = new Connector();
		if (isset($filter['name']) && $filter['name']) {
			$connector->whereAdd("NAME LIKE '%".mysql_real_escape_string($filter['name'])."%'");
		}
        if (isset($filter['id_provider']) && $filter['id_provider']) {
			$connector->ID_PROVIDER = $filter['id_provider'];
		}
		$connector->orderBy("NAME");
		if ($pager == true) {
			$pager = new DBPager($connector, $filter, true);
		}
		$connector->find();
		return $connector;
	}

	public function isExistsByName($name= '', $id = 0)
	{
		if ($name == '')
		{
			return false;
		}
		$connector = new Connector();
		if ($id) {
			$connector->whereAdd("ID <> {$id}");
		}
		$connector->NAME = $name;
		$connector->find();
		return $connector->N > 0;
	}


	public function validateInput(){
		global $error_msg;

		if (!textvalid($this->NAME))
		{
			$error_msg->add(_MSG_CONNECTOR_NAME_NOT_EMPTY);
		}
		if(!Utils::isValidLength($this->NAME, MAX_LENGTH_NAME))
		{
			$error_msg->add(_LB_INVALID_LENGTH_CODE);
		}
		if (self::isExistsByName($this->NAME, $this->ID))
		{
			$error_msg->add(_MSG_CONNECTOR_NAME_EXISTS);
		}
		if (!textvalid($this->URL)) {
			$error_msg->add(_MSG_CONNECTOR_URL_NOT_EMPTY);
		}
        if (!checkValidUrl($this->URL))
		{
			$error_msg->add(_MSG_INVALID_CONNECTOR_URL);
		}
		if ($this->TYPE != CONNECTOR_GENERIC && $this->TYPE != CONNECTOR_SPECIFIC) {
			$error_msg->add(_MSG_CONNECTOR_TYPE_VALID);
		}
		if (!textvalid($this->FREQUENCY)) {
			$error_msg->add(_MSG_CONNECTOR_FREQUENCY_NOT_EMPTY);
		}
		if ($this->FREQUENCY <= 0 || !is_numeric($this->FREQUENCY)) {
			$error_msg->add(_MSG_DEVICE_MODEL_SCREEN_WIDTH_INVALID_NUMBER);
		}
        if ($this->TYPE == CONNECTOR_SPECIFIC) {

            if (!textvalid($this->CLASS_NAME)) {
                $error_msg->add(_MSG_CONNECTOR_CLASS_NAME_NOT_EMPTY);
            }
            
            if (!self::isExistsByClassName($this->CLASS_NAME)) {
                $error_msg->add(_MSG_CONNECTOR_CLASS_NAME_INVALID);
            }
            
            if ($this->CLASS_NAME == 'AggregateDealYelp' && !$this->COUNTRY_CODE) {
                $error_msg->add(_MSG_COUNTRY_INVALID);
            }
		
            if (($this->CLASS_NAME == 'AggregateDealLivingSocial' || $this->CLASS_NAME == 'AggregateDealYelp') && !preg_match('/^[a-z]{2}(_[A-Z]{2})*?$/', $this->LANGUAGE_CODE)) {
                $error_msg->add(_MSG_COUNTRY_LANGUAGE_CODE_INVALID);
            }
        }
        
        if ($this->COUNTRY_CODE && !Country::isExistsByCode($this->COUNTRY_CODE)) {
            $error_msg->add(_MSG_COUNTRY_NOT_EXISTS);
        }
		
		return $error_msg->isNoError();
	}
    public function getType($id_connector)
    {
        $connector = new Connector();
		if ($id_connector) {
			$connector->ID = $id_connector;
		}
        $connector->find();
        if ($connector->N > 0) {
            while ($connector->fetch())
            {
                return $connector->TYPE;
            }
        }
    }

    public function isExistsByClassName($class_name = '')
	{
		if ($class_name == ''){
			return false;
}
        
        $file_dir_classname =  APPLICATION_DIR . "/application/controller/scripts/";
        $classname_array = Provider_Connector::getlistClassAggregateDeal($file_dir_classname);
        if (in_array($class_name, $classname_array)){
            return true;
        }
        
        return false;
	}  

}
